package forecasting.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileUtils {
	
	private FileUtils() {}
	
	/**
	 * Utility method that's used for generating a {@link Scanner} for an existing file.
	 * @param filename
	 * @return Scanner
	 */
	public static Scanner openFileScanner(final String filename) {
		final File file = new File(filename);
		if(!file.exists()) {
			System.err.println(filename + " does not exist");
			System.exit(-2);
		}
		
		try {
			return new Scanner(file);
		} catch (FileNotFoundException e1) {
			System.err.println("Cannot create scanner for " + filename);
			System.exit(-3);
		}
		return null;
	}
	
	public static List<String> scannerToList(final Scanner scanner) {
		if (scanner == null) {
			return null;
		}
		
		final List<String> list = new ArrayList<String>();
		
		while (scanner.hasNextLine()) {
			list.add(scanner.nextLine());
		}
		
		return list;		
	}


}
